﻿using AJWPFAdmin.Core.Enums;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static AJWPFAdmin.Core.Models.EntryConfig;

namespace AJWPFAdmin.Core.Models
{
    /// <summary>
    /// 出入控制配置
    /// </summary>
    public class EntryConfig : BindableBase
    {
        public class EmissionStandardCfgItem : BindableBase
        {
            private bool _checked;
            public bool Checked
            {
                get { return _checked; }
                set { SetProperty(ref _checked, value); }
            }

            public EmissionStandard _standard;
            public EmissionStandard Standard
            {
                get { return _standard; }
                set { SetProperty(ref _standard, value); }
            }
        }

        public class CarTypeEnumCfgItem : BindableBase
        {
            private bool _inChecked;
            public bool InChecked
            {
                get { return _inChecked; }
                set { SetProperty(ref _inChecked, value); }
            }

            private bool _outChecked;
            public bool OutChecked
            {
                get { return _outChecked; }
                set { SetProperty(ref _outChecked, value); }
            }

            public CarTypeEnum _carType;
            public CarTypeEnum CarType
            {
                get { return _carType; }
                set { SetProperty(ref _carType, value); }
            }
        }

        private List<EmissionStandardCfgItem> _emissionStandardCfgItems;
        /// <summary>
        /// 排放标准自动入厂配置
        /// </summary>
        public List<EmissionStandardCfgItem> EmissionStandardCfgItems
        {
            get { return _emissionStandardCfgItems; }
            set { SetProperty(ref _emissionStandardCfgItems, value); }
        }

        private List<CarTypeEnumCfgItem> _carTypeEnumCfgItems;
        /// <summary>
        /// 车辆类型自动入厂配置
        /// </summary>
        public List<CarTypeEnumCfgItem> CarTypeEnumCfgItems
        {
            get { return _carTypeEnumCfgItems; }
            set { SetProperty(ref _carTypeEnumCfgItems, value); }
        }

        private bool _appointCheck;
        /// <summary>
        /// 运输车辆每次预约入厂, 接口返回数据才自动入厂, 就算有台账数据也不能自动
        /// </summary>
        public bool AppointCheck
        {
            get { return _appointCheck; }
            set { SetProperty(ref _appointCheck, value); }
        }

        private bool _disableGateControl;
        /// <summary>
        /// 是否禁用开关闸命令传输
        /// </summary>
        public bool DisableGateControl
        {
            get { return _disableGateControl; }
            set { SetProperty(ref _disableGateControl, value); }
        }

        private DateTime? _belowOrEqualStandard5PassStartTime;
        /// <summary>
        /// 国五及以下车辆进厂开始时段
        /// </summary>
        public DateTime? BelowOrEqualStandard5PassStartTime
        {
            get => _belowOrEqualStandard5PassStartTime;
            set => SetProperty(ref _belowOrEqualStandard5PassStartTime, value);
        }

        private DateTime? _belowOrEqualStandard5PassEndTime;
        /// <summary>
        /// 国五及以下车辆进厂结束时段
        /// </summary>
        public DateTime? BelowOrEqualStandard5PassEndTime
        {
            get => _belowOrEqualStandard5PassEndTime;
            set => SetProperty(ref _belowOrEqualStandard5PassEndTime, value);
        }

        private bool _belowOrEqualStandard5PassEndTimeInNextDay;
        /// <summary>
        /// 国五及以下车辆进厂结束时段是否是次日
        /// </summary>
        public bool BelowOrEqualStandard5PassEndTimeInNextDay
        {
            get => _belowOrEqualStandard5PassEndTimeInNextDay;
            set => SetProperty(ref _belowOrEqualStandard5PassEndTimeInNextDay, value);
        }

        private FreightUnit _defaultFreightUnit;
        /// <summary>
        /// 默认货运单位
        /// </summary>
        public FreightUnit DefaultFreightUnit
        {
            get => _defaultFreightUnit;
            set => SetProperty(ref _defaultFreightUnit, value);
        }

        public EntryConfig()
        {
            EmissionStandardCfgItems = new List<EmissionStandardCfgItem>();
            CarTypeEnumCfgItems = new List<CarTypeEnumCfgItem>();
        }

        public void Prepare()
        {
            var carTypeEnums = Enum.GetValues(typeof(CarTypeEnum));
            var carTypeEnumsSource = new List<CarTypeEnum>();

            var source = new List<CarTypeEnumCfgItem>();
            source.AddRange(CarTypeEnumCfgItems);

            foreach (CarTypeEnum carType in carTypeEnums)
            {
                carTypeEnumsSource.Add(carType);

                if (!source.Any(p => p.CarType == carType))
                {
                    source.Add(new CarTypeEnumCfgItem
                    {
                        InChecked = carType < CarTypeEnum.非道路移动机械,
                        OutChecked = carType < CarTypeEnum.非道路移动机械,
                        CarType = carType
                    });
                }

            }

            foreach (var item in CarTypeEnumCfgItems)
            {
                if (!carTypeEnumsSource.Any(p => p == item.CarType))
                {
                    source.RemoveAll(p => p.CarType == item.CarType);
                }
            }

            CarTypeEnumCfgItems = source;
        }

        public void InitDefault()
        {
            EmissionStandardCfgItems = new List<EmissionStandardCfgItem>();
            var enums = Enum.GetValues(typeof(EmissionStandard));
            foreach (EmissionStandard item in enums)
            {
                EmissionStandardCfgItems.Add(new EmissionStandardCfgItem
                {
                    Checked = item == EmissionStandard.国五 || item == EmissionStandard.国六,
                    Standard = item
                });
            }
            CarTypeEnumCfgItems = new List<CarTypeEnumCfgItem>();
            var carTypeEnums = Enum.GetValues(typeof(CarTypeEnum));
            foreach (CarTypeEnum carType in carTypeEnums)
            {

                CarTypeEnumCfgItems.Add(new CarTypeEnumCfgItem
                {
                    InChecked = carType < CarTypeEnum.非道路移动机械,
                    OutChecked = carType < CarTypeEnum.非道路移动机械,
                    CarType = carType
                });
            }
        }
    }
}
